<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_produk extends CI_Controller {
    private $limit = 20;
    function __construct() {
        parent::__construct();
        $this->load->model('model_produk', 'm_prod', TRUE);
        $this->load->model('model_gallery', 'm_gall', TRUE);
    }
    
    function index() {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        if($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        $data = array(
            'web_title' =>  'Admin | List All Product',
            'main_view' =>  'mimin/product/index_new',
            'list_data' =>  $this->m_prod->get_all()
        );
        
        $this->load->view('mimin/template/template_table',$data);
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            $this->form_validation->set_rules('judul','Judul','required');
            $this->form_validation->set_rules('content','Isi Konten','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $condition  = " `type` = 'i'";
                $data       = array(
                    'web_title'             =>  'Data Produk Baru',
                    'main_view'             =>  "mimin/product/add_new",
                    'list_data'             =>  $this->m_gall->get_data_by_condition($condition)
                );
                $this->parser->parse("mimin/template/template_editor",$data);
            }
            else{
                $config['upload_path'] = './content/media/events/';
                $config['allowed_types'] = 'jpg|jpeg|png';
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()){
                    echo"sini".$this->upload->display_errors();exit();
                    $data = array(
                        "main_view"     =>  "mimin/event/add_new",
                        "web_title"     =>  "Add New Event Data",
                        "error"         =>  $this->upload->display_errors()
                    );
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d h:i:s");
                    $upload_image = $this->upload->data();
                    $isi = array(
                        'judul'         =>  $this->input->post('judul'),
                        'content'       =>  $this->input->post('content'),
                        'eventdate'     =>  $this->input->post('tanggal_event'),
                        'createdby'     =>  $this->session->userdata('admin_user')->id_admin,
                        'datecreated'   =>  $curdate,
                        'datemodified'  =>  $curdate,
                        'place'         =>  $this->input->post('place'),
                        'state_id'      =>  $this->session->userdata('admin_user')->state_id,
                        'image'         =>  'content/media/events/'.$upload_image['file_name']
                    );
                    if($this->m_event->save($isi)){
                        $data = array(
                            "main_view"         =>  "mimin/event/add_new",
                            "web_title"         =>  "Successfull Add new Event Data",
                            "success_status"    =>  1
                        );
                        $this->load->view("mimin/template/template_editor",$data);
                    }
                }
            }
        }
    }
    function delete($id=""){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_event","refresh");
        }
        else{
            //JIKA koordinator Propinsi
            if($this->session->userdata('admin_user')->level == 2){
                //cek database 
                $list_event = $this->m_event->select_by_id($id);
                $stateid="";
                foreach ($list_event as $r){
                    $stateid = $r->state_id;
                }
                
                if($this->session->userdata('admin_user')->state_id == $stateid){
                    if($this->m_event->delete($id)){
                        redirect("admin_event","refresh");
                    }
                    else{
                        redirect("admin_event","refresh");
                    }
                }
                else{
                    redirect("admin_event","refresh");
                }
            }
            else{
                if($this->m_event->delete($id)){
                    redirect("admin_event","refresh");
                }
            }
        }
    }
    function detail($id=""){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_event","refresh");
        }
        else{
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            
            $count = $this->m_event->select_by_id($id,"count");
     
            if($count > 0){
                $this->form_validation->set_rules('judul','Judul','required');
                $this->form_validation->set_rules('content','Isi Konten','required');
                if($this->form_validation->run() == FALSE){
                    $data = array(
                        "main_view"     =>  "mimin/event/detail",
                        "web_title"     =>  "Data Event Detail",
                        "list_data"     =>  $this->m_event->select_by_id($id)
                    );            
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    $config['upload_path'] = './content/media/events/';
                    $config['allowed_types'] = 'jpg|jpeg|png';
                    $this->load->library('upload', $config);
                    
                    //IF THE ADMIN Do Not Choose any image to upload
                    if (!$this->upload->do_upload()){
                        //echo"sini".$this->upload->display_errors();exit();
                        $isi_update = array(
                            "judul"         =>  $this->input->post("judul"),
                            "place"         =>  $this->input->post("place"),
                            "content"       =>  $this->input->post("content"),
                            "datemodified"  =>  $curdate,
                            "modifiedby"    =>  $this->session->userdata('admin_user')->id_admin,
                            "eventdate"     =>  $this->input->post("tanggal_event"),
                        );
                        $status_update = $this->m_event->update($id,$isi_update);
                        if($status_update == false){
                            echo"false";
                        }
                        else{
                            $data = array(
                                "main_view"         =>  "mimin/event/detail",
                                "web_title"         =>  "Successfull Update Event Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_event->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        $data = array(
                            "main_view"     =>  "mimin/event/add_new",
                            "web_title"     =>  "Add New Event Data",
                            "error"         =>  $this->upload->display_errors()
                        );
                        //$this->load->view("mimin/template/template_editor",$data);
                    }
                    else{
                        //echo"sini";exit();
                        
                        $upload_image = $this->upload->data();
                        $isi_update = array(
                            'judul'         =>  $this->input->post('judul'),
                            'content'       =>  $this->input->post('content'),
                            'datemodified'  =>  $curdate,
                            'place'         =>  $this->input->post('place'),
                            "modifiedby"    =>  $this->session->userdata('admin_user')->id_admin,
                            'image'         =>  'content/media/events/'.$upload_image['file_name'],
                            "eventdate"     =>  $this->input->post("tanggal_event"),
                        );
                        
                        $status_update = $this->m_event->update($id,$isi_update);
                        if($status_update){
                            $data = array(
                                "main_view"         =>  "mimin/event/detail",
                                "web_title"         =>  "Successfull Add new Event Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_event->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        else{
                            echo"Cannot update data";
                        }
                    }
                }
            }
            else{
                redirect("admin_event","refresh");
            }
        }
    }
}

/* End of file admin_produk.php */
/* Location: ./application/controllers/admin_produk.php */